package cn.spring.framework.redis.mock;

import java.lang.reflect.Method;

/**
 * @author xingkong
 * @Title: MatchKeyWrapperMock
 * @Description:
 * @date 2021/11/18 15:46
 */
public class MatchKeyWrapperMock implements Mock {
    /**
     * 目标key
     */
    private String targetKey;

    /**
     * mock实现
     */
    private Mock mock;

    public MatchKeyWrapperMock(String targetKey, Mock mock) {
        this.targetKey = targetKey;
        this.mock = mock;
    }

    /**
     * 降级
     *
     * @param key    键值
     * @param method 方法
     * @return
     */
    @Override
    public Object mock(String key, Method method) {
        if (key == null) {
            throw new IllegalArgumentException("查询的 key 不能为空");
        }
        if (targetKey == null) {
            return mock.mock(key, method);
        }
        if (targetKey.equals(key)) {
            return mock.mock(key, method);
        }
        return null;
    }
}
